package com.example.teacherservice.repository;

import com.example.teacherservice.entity.Course;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface CourseRepository extends JpaRepository<Course, Long> {
    List<Course> findByTeaId(Long teaId);

    // 新增查询所有课程方法
    // 基本查询（不加载关联）
    @Query("SELECT c FROM Course c WHERE c.deleted = 0")
    List<Course> findAllActiveCourses();

    @Query("SELECT new map(c.id as id, c.couName as name) FROM Course c WHERE c.id IN :ids AND c.deleted = 0")
    List<Map<String, Object>> findCourseNamesByIds(@Param("ids") List<Long> ids);

    @Modifying
    @Query("UPDATE Course c SET c.couCataNum = c.couCataNum + :increment WHERE c.id = :courseId")
    void incrementChapterCount(@Param("courseId") Long courseId, @Param("increment") int increment);

    // 根据课程ID列表查询课程基本信息
    @Query("SELECT new map(" +
            "c.id as id, " +
            "c.couName as name, " +
            "c.couPic as coverUrl, " +
            "c.couPrice as price, " +
            "c.couIntroduction as introduction, " +
            "c.purchase as purchaseCount) " +
            "FROM Course c " +
            "WHERE c.id IN :ids AND c.deleted = 0")
    List<Map<String, Object>> findCourseBasicInfoByIds(@Param("ids") List<Long> ids);

}